package ch.manage.movie.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import ch.manage.movie.business.Movie;

public class MovieList extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// TEST INPUT

	private final String[] columnNames = { "Title", "Released", "Rated", "Duration", "Genre", "Director", "Writer", "Actors", "Plot", "Poster", "Rating", "Filepath", "Seen" };
	private Object[][] tableData;
	private static List<Movie> movieList;
	private JTable movieTable;

	public MovieList(final MovieInfo movieInfo, List<Movie> movieList) {
		MovieList.movieList = movieList;
		tableData = new Object[movieList.size()][columnNames.length];
		setLayout(new BorderLayout(0, 0));

		JLabel lblNewLabel = new JLabel("MOVIE LIST");
		add(lblNewLabel, BorderLayout.NORTH);
		
		movieTable = new JTable(tableData, columnNames);
		movieTable.setDoubleBuffered(true);
		movieTable.setAutoCreateRowSorter(true);
		movieTable.getTableHeader().addMouseListener(new ColumnChooser());
		JScrollPane scrollPane = new JScrollPane(movieTable);
		movieTable.setFillsViewportHeight(true);
		movieTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		movieTable.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent e) {
							if(movieTable.getSelectedRow()!=-1){
								movieInfo.setMovieTitle(movieTable.getValueAt(movieTable.getSelectedRow(), 0).toString());
								movieInfo.setMovieCover((BufferedImage) movieTable.getValueAt(movieTable.getSelectedRow(), 9));
								movieInfo.setMovieGenre(movieTable.getValueAt(movieTable.getSelectedRow(), 4).toString());
								movieInfo.setMovieDescription(movieTable.getValueAt(movieTable.getSelectedRow(), 8).toString());
								movieInfo.setMovieReleased(movieTable.getValueAt(movieTable.getSelectedRow(), 1).toString());
								movieInfo.repaint();
							}
							return;
					}
				});
		fillTableData();
		this.add(scrollPane, BorderLayout.CENTER);
	}

	private class ColumnChooser extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			if (e.isMetaDown()) {
				JPopupMenu columnChooserPopup = new JPopupMenu();
				ImageIcon enabled = new ImageIcon("./icons/enabled.jpg");
				ImageIcon disabled = new ImageIcon("./icons/disabled.jpg");
				JMenuItem menuItem = null;
				TableColumn colToBeChecked;
				for (int i = 0; i < columnNames.length; ++i) {
					colToBeChecked = movieTable.getColumn(columnNames[i]);
					if (colToBeChecked.getWidth() != 0) {
						menuItem = new JMenuItem(columnNames[i], enabled);
					} else {
						menuItem = new JMenuItem(columnNames[i], disabled);
					}

					menuItem.setActionCommand(i + "");
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent ae) {
							TableColumn colToBeChanged = movieTable
									.getColumn(columnNames[Integer.parseInt(ae
											.getActionCommand())]);
							if(colToBeChanged == movieTable.getColumn("Title")){
								return;
							}else if (colToBeChanged.getWidth() != 0) {
								colToBeChanged.setMaxWidth(0);
								colToBeChanged.setMinWidth(0);
								colToBeChanged.setPreferredWidth(0);
							} else {
								colToBeChanged
										.setPreferredWidth(Integer.MAX_VALUE);
								colToBeChanged.setMaxWidth(Integer.MAX_VALUE);
								colToBeChanged.sizeWidthToFit();
							}
						}
					});
					columnChooserPopup.add(menuItem);
				}
				columnChooserPopup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
	
	public void updateMovieList(List<Movie> movieList){
		MovieList.movieList = movieList;
		fillTableData();
		movieTable.revalidate();
	}
	
	private void fillTableData(){
		TableModel tableModel = movieTable.getModel();
		for (int i = 0; i < movieList.size(); ++i) {
				tableModel.setValueAt(movieList.get(i).getTitle(), i, 0);
				tableModel.setValueAt(movieList.get(i).getPath(), i, 11);			
				tableModel.setValueAt(movieList.get(i).getRated(), i, 2);
				tableModel.setValueAt(movieList.get(i).getDuration(), i, 3);
				tableModel.setValueAt(movieList.get(i).getGenre(), i, 4);
				tableModel.setValueAt(movieList.get(i).getDirectors(), i, 5);
				tableModel.setValueAt(movieList.get(i).getWriters(), i, 6);
				tableModel.setValueAt(movieList.get(i).getActors(), i, 7);
				tableModel.setValueAt(movieList.get(i).getDescription(), i, 8);
				tableModel.setValueAt(movieList.get(i).getCover(), i, 9);
				tableModel.setValueAt(movieList.get(i).getRating(), i, 10);
				tableModel.setValueAt(movieList.get(i).getRelease(), i, 1);
				tableModel.setValueAt(movieList.get(i).isSeen(), i, 12);
		}
		((AbstractTableModel) tableModel).fireTableDataChanged();
	}
	
	public static List<Movie> getMovieList() {
		return movieList;
	}

}
